<?php

namespace App\Models;

use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Laravel\Lumen\Auth\Authorizable;
use DateTimeInterface;

class Crm_Meet extends Model implements AuthenticatableContract, AuthorizableContract
{
    use Authenticatable, Authorizable, HasFactory;
    /**
     * 关联到模型的数据表
     *
     * @var string
     */
    protected $table = 'crm_meet';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    protected $fillable = [
        'agencyid',
        'mid',
        'otherid',
        'type',
        'option_id',
        'option_name',
        'status'
    ];

    protected $hidden = [
        'is_del',
    ];

    public function scopeNodel($query)
    {
        return $query->where('is_del', 'N');
    }

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }

    public function hasOneMember()
    {
        return $this->hasOne('App\Models\Crm_Member', 'id', 'mid');
    }

    public function hasOneOtherMember()
    {
        return $this->hasOne('App\Models\Crm_Member', 'id', 'otherid');
    }

    public static function getFilter($filter)
    {
        return self::where(function ($query) use ($filter){
            if (isset($filter['mid']) && !empty($filter['mid'])) {
                $query->where('mid',  $filter['mid'] );
            }
            if (isset($filter['otherid']) && !empty($filter['otherid'])) {
                $query->where('otherid',  $filter['otherid'] );
            }
            if (isset($filter['startTime']) && !empty($filter['startTime'])) {
                $query->where('created_at', '<=', $filter['startTime']);
            }
            if (isset($filter['endTime']) && !empty($filter['endTime'])) {
                $query->where('created_at', '>=',$filter['endTime']);
            }
            $query->where('agencyid',  $filter['aid']);
        });
    }
}
